package com.example.onlineeducation.ui.mine

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.entity.ProInfoEntity
import com.example.onlineeducation.databinding.ProjectListAcitvityBinding
import com.example.onlineeducation.databinding.ProjectListItemBinding
import com.example.onlineeducation.viewmodel.ProjectVM
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.extension.ycInitLinearLayoutManage
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.refresh.YcRefreshSpecialUtil
import com.yc.jetpacklib.release.YcSpecialViewSmart
import com.yc.jetpacklib.utils.YcResources


@SuppressLint("SetTextI18n")
class ProListActivity : BaseActivity<ProjectListAcitvityBinding>(ProjectListAcitvityBinding::inflate) {
    private val mViewModel: ProjectVM by ycViewModels()
    private lateinit var mRefreshUtil: YcRefreshSpecialUtil

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, ProListActivity::class.java)
            context.startActivity(intent)
        }
    }
    private val mProAdapter: YcRecyclerViewAdapter<ProInfoEntity, ProjectListItemBinding> by YcRecyclerViewAdapter.ycLazyInitApply(
        ProjectListItemBinding::inflate
    ) {
        mOnUpdate = { item ->
            tvProName.text = item.proName
            tvProStatus.text = "(${item.proStatusName})"
            if (item.proStatusIsOK) {
                tvProStatus.setTextColor(YcResources.getColorRes(R.color.colorPrimary))
            } else {
                tvProStatus.setTextColor(YcResources.getColorRes(R.color.red))
            }
        }
        mItemClick = { item ->
            if (item.proId != App.mInstant.mViewModel.mProInfoEntity.value?.proId) {
                if (item.proStatusIsOK) {
                    App.mInstant.mViewModel.refreshProInfo(item)
                    finish()
                } else {
                    showToast("请选择状态是正常的项目")
                }
            }
        }
    }


    override fun ProjectListAcitvityBinding.initView() {
        actionbarLayout.setTitle(titleName = "切换项目")
        rvProList.apply {
            adapter = mProAdapter
            ycInitLinearLayoutManage()
        }
        srlList.setNoMoreData(true)
        mRefreshUtil = YcRefreshSpecialUtil(this@ProListActivity, srlList, mProAdapter, YcSpecialViewSmart(rvProList, flRefresh))
        mRefreshUtil.mRefreshAndLoadMore = { isRefresh: Boolean, pageIndex: Int, pageSize: Int ->
            if (isRefresh) {
                getData()
            }
        }
        mViewModel.mUerInfo.observe {
            it.doSuccess { data ->
                if (data.userProInfoList.ycIsNotEmpty()) {
                    mProAdapter.addAllData(data.userProInfoList)
                }
            }.ycShowNetError()
            mRefreshUtil.ycRefreshAndLoadMoreResult(it)
        }
    }

    private fun getData() {
        mViewModel.getUerInfo()
    }
}